package com.cl.edu.service;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cl.core.orm.Page;
import com.cl.edu.dao.LinksDAO;
import com.cl.edu.dao.NewsDAO;
import com.cl.edu.dao.TeacherDAO;
import com.cl.edu.dao.WeekstarDAO;
import com.cl.edu.entity.Links;
import com.cl.edu.entity.News;
import com.cl.edu.entity.Teacher;
import com.cl.edu.entity.WeekStar;

@Service
@Transactional
public class IndexManager {
	@Autowired
	private NewsDAO newsDAO;
	
	@Autowired
	private WeekstarDAO weekstarDAO;
	@Autowired
	private TeacherDAO teacherDAO;
	
	@Autowired
	private LinksDAO linksDAO;
	/**
	 * 通过栏目id获取前n条数据
	 * @param page
	 * @param id
	 * @param n
	 * @return
	 */
	public Page<News> getNewsById(Page<News> page,Long id,int n) {
		StringBuffer hql = new StringBuffer("from News model ");
		hql.append(" where model.channel.id = ").append(id);
		page.setPageSize(n);
		hql.append(" order by model.").append(page.getOrderBy()).append(" ").append(page.getOrder());
		page = newsDAO.findPage(page, hql.toString());

		return page; 
	}
//	public Page<WeekStar> getWeekStar(Page<WeekStar> page){
//		StringBuffer hql = new StringBuffer("from WeekStar model ");
//		page.setPageSize(8);
//		hql.append(" order by model.").append(page.getOrderBy()).append(" ").append(page.getOrder());
//		page = weekstarDAO.findPage(page, hql.toString());
//		return page;
//	}
	/**
	 * 修改了之前的一周之星  暂没用weekstar表
	 */
	public Page<Teacher> getWeekStar(Page<Teacher> page){
		StringBuffer hql = new StringBuffer("from Teacher model where model.isWeekStar > 0");
		hql.append(" order by model.isWeekStar asc");
		page.setPageSize(8);
		page = teacherDAO.findPage(page, hql.toString());
		return page;
	}
	
	/**
	 * 外部链接列表
	 * @return
	 */
	public List<Links> getLinkList(){
		String hql = "from Links model where model.display = 1 order by model.id asc";
		return linksDAO.find(hql); 
	}
}
